package xgrammar;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.w3c.xqparser.ParseException;
import org.w3c.xqparser.SimpleNode;
import org.w3c.xqparser.XParser;
import org.w3c.xqparser.XParserVisitor;

public class Main
{
    public static final int PRINT_TEXT=0;
    public static final int PRINT_TABLE=1;
    public static final int PRINT_XML=2;

    protected static String expr="";
    protected static int format=PRINT_TEXT;

    public static void main(String[] args) {

        try
        {
            parseArguments(args);

            if (expr.length()==0)
            {
                showHelp(System.err);
                System.exit(1);
            }

            XParser parser = new XParser(new java.io.StringReader(expr));
            SimpleNode tree = parser.START();

            if (tree==null)
            {
                System.err.println("CRITICAL: No parse tree.");
                System.exit(1);
            }
            else
            {
                /*ByteArrayOutputStream baos = new ByteArrayOutputStream(62 * 1024);
                PrintStream ps = new PrintStream(baos);
                tree.dump("|", ps);
                String s = new String(baos.toByteArray());
                System.out.println(s); */

                XParserVisitor vp=null;

                switch (format)
                {
                    case PRINT_TABLE:vp=new vxpPrintTable();break;
                    case PRINT_TEXT: vp=new vxpPrintText(); break;
                    case PRINT_XML:  vp=new vxpPrintXML();  break;
                }

                tree.jjtAccept(vp,0);
            }
        }
        catch (ParseException e)
        {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        catch (Error err)
        {
            String msg = err.getMessage();
            if (msg == null || msg.equals(""))
                msg = "Unknown Error: " + err.getClass().getName();

            System.err.println(msg);
            System.exit(1);
        }

        System.exit(0);
    }

    public static void showHelp()
    {
        showHelp(System.out);
        System.exit(0);
    }

    public static void showHelp(PrintStream p)
    {
        p.println("Usage: [options] <xpath>");
        p.println("\nOptions:");
        p.println("\t--table\tPrint a table of nodes");
        p.println("\t--text\tPrint as indented text");
        p.println("\t--xml\tPrint as XML");
        p.println();
    }

    public static void parseArguments(String[] args)
    {
        for (int i=0;i<args.length;i++)
            if (args[i].equals("--text"))
                format=PRINT_TEXT;
            else
            if (args[i].equals("--table"))
                format=PRINT_TABLE;
            else
            if (args[i].equals("--xml"))
                format=PRINT_XML;
            else
            if (args[i].equals("--help"))
                showHelp();
            else
                expr=args[i];
    }
}
